package com.zkca.xf.ui.activity.technicalprotect;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.MyApplication;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseActivity;
import com.zkca.xf.databinding.ActivityWaterXBinding;
import com.zkca.xf.model.javabean.WaterLBean;
import com.zkca.xf.model.javabean.uploadbean.RequestPage;
import com.zkca.xf.presenter.WaterLpresenter;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.TimeUtils;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.FireAlarmLView;
import com.zkca.xf.view.OnClickView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Water_XActivity extends BaseActivity implements OnClickView, FireAlarmLView {

    String telNum;
    ActivityWaterXBinding binding;
    WaterLBean.DataBean dataBean;

    private RequestPage requestPage;
    private WaterLpresenter fireAlarmLPresenter;
    private List<WaterLBean.DataBean> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_water__x);
        binding = DataBindingUtil.setContentView(
                this, R.layout.activity_water__x);
        Intent intent = getIntent();

        fireAlarmLPresenter=new WaterLpresenter(this,this);


        boolean xg = intent.hasExtra("XG");
        if (xg) {
            int id = intent.getIntExtra("XG", 0);
            initResdata(id);

        } else {
            dataBean = (WaterLBean.DataBean) intent.getSerializableExtra("waterbean");

            bibdingdata();
        }


    }

    private void initResdata(int id) {
        Map map = new HashMap<String, Object>();
        map.put("id", id+"");
        requestPage = new RequestPage();
        requestPage.setAccessId(SPUtil.getString(this, Constants_SP.ACCESSID));
        requestPage.setPageNum(1);
        requestPage.setPageSize(10);
        requestPage.setParam(map);
        requestPage.setTimeStamp(TimeUtils.getTime());
        requestPage.setSign(MyApplication.getInstance().generateSign(requestPage));
        fireAlarmLPresenter.getShui(requestPage);
    }


    private void bibdingdata() {

        binding.setDianliu(dataBean);
        binding.setCallback(this);
        long alarmTime = dataBean.getCreateTime();
        String formatDate2 = TimeUtils.getFormatDate2(alarmTime);
        telNum = dataBean.getTelephone();
        binding.alarmtime.setText(formatDate2);
        if (dataBean.getValueType()==0){
            binding.valuetype.setText("液位");
        }else {
            binding.valuetype.setText("压力");
        }
        if (dataBean.getDevStatus() == 0) {
            binding.devstatus.setText("正常");

        } else if (dataBean.getDevStatus() == 1) {
            binding.devstatus.setText("预警");

        } else if (dataBean.getDevStatus() == 2) {
            binding.devstatus.setText("报警");

        } else if (dataBean.getDevStatus() == 3) {
            binding.devstatus.setText("断线");

        } else if (dataBean.getDevStatus() == 4) {
            binding.devstatus.setText("异常");

        } else if (dataBean.getDevStatus() == 5) {
            binding.devstatus.setText("故障");

        }

    }

    @Override
    public void click1(View v) {
        switch (v.getId()) {

            case R.id.ll_basetitle_back:
                finish();
                break;
        }
    }

    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void getFireAlarm(String s) {
        Gson gson = new Gson();
        WaterLBean friealarmLbean = gson.fromJson(s, WaterLBean.class);
        if (friealarmLbean.getResult() == 0) {
            List<WaterLBean.DataBean> data = friealarmLbean.getData();
            if (data != null && data.size() > 0) {
                dataList.addAll(data);
                dataBean = dataList.get(0);
                bibdingdata();
            }
        } else {
            ToastUtils.show(this, friealarmLbean.getInfo());
        }
    }
}
